package com.yiqi.lottery.feature.lottery.view;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import android.widget.TextView;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.BaseFragment;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.utils.logger.Logger;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.lottery.R;
import com.yiqi.lottery.entity.bo.HomeBo;
import com.yiqi.lottery.feature.lottery.contract.HomeContract;
import com.yiqi.lottery.feature.lottery.dependencies.home.DaggerHomeComponent;
import com.yiqi.lottery.feature.lottery.dependencies.home.HomePresenterModule;
import com.yiqi.lottery.feature.lottery.view.adapter.HomeAdapter;
import com.yiqi.lottery.feature.lottery.view.adapter.MultipleItem;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import javax.inject.Inject;


public class LotteryFragment extends BaseFragment implements HomeContract.View {
    HomeAdapter mHomeAdapter = null;

    @BindView(R.id.rv_home)
    RecyclerView rvHome;
    @BindView(R.id.tv_main)
    TextView tvHOme;
    private CallBackValue callBackValue;
    EmptyView mEmptyView;
    @Inject
    HomeContract.Presenter mPresenter;


    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_lottery;
    }

    @Override
    protected void initInjector() {
        DaggerHomeComponent.builder().homePresenterModule(new HomePresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        mEmptyView = new EmptyView(rvHome);
        rvHome.setLayoutManager(new LinearLayoutManager(getActivity()));
        tvHOme.setText("购彩大厅");

        initHomeList(null);
    }


    @Override
    protected void initData() {
        mPresenter.getHomeData();



    }

    private void initHomeList(HomeBo homeBo) {

        List<MultipleItem> list = new ArrayList<>();
        list.add(new MultipleItem(MultipleItem.ITEM_BANNER, MultipleItem.IMG_SPAN_SIZE));
        list.add(new MultipleItem(MultipleItem.ITEM_MSG, MultipleItem.IMG_SPAN_SIZE, "奥什市哦"));
        list.add(new MultipleItem(MultipleItem.ITEM_TYPE, MultipleItem.IMG_SPAN_SIZE));
        list.add(new MultipleItem(MultipleItem.ITEM_LUCKY_NUMBER, MultipleItem.IMG_SPAN_SIZE));
        list.add(new MultipleItem(MultipleItem.ITEM_WINNING_COLUMN, MultipleItem.IMG_SPAN_SIZE));
        list.add(new MultipleItem(MultipleItem.ITEM_FOCUS_EVENT, MultipleItem.IMG_SPAN_SIZE));
        mHomeAdapter = new HomeAdapter(homeBo, list, getActivity());
        rvHome.setAdapter(mHomeAdapter);
        mHomeAdapter.setReplcae(new HomeAdapter.ReplaceFragmentLisnetaner() {
            @Override
            public void doReplace() {
                callBackValue.SendMessageValue(true);
            }
        });
    }


    @Override
    public void onStart() {
        super.onStart();

        if (mHomeAdapter != null)
            mHomeAdapter.onStart();

    }

    @Override
    public void onResume() {
        super.onResume();

        if (mHomeAdapter != null)
            mHomeAdapter.onResume();

    }

    @Override
    public void onPause() {
        super.onPause();

        if (mHomeAdapter != null)
            mHomeAdapter.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();

        if (mHomeAdapter != null)
            mHomeAdapter.onStop();
    }

    @Override
    protected void initEvent() {

    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        T.showShort("Hidden");
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        //当前fragment从activity重写了回调接口  得到接口的实例化对象
        callBackValue = (CallBackValue) getActivity();
    }

    @Override
    public void showSuccess(HomeBo s) {
        initHomeList(s);
    }

    @Override
    public void showError(String ex) {
        T.showShort(ex);
        mHomeAdapter.setEmptyView(mEmptyView.getEmptyView());

    }

    //定义一个回调接口
    public interface CallBackValue {
        void SendMessageValue(boolean isShowOrder);
    }
}
